// pages/goods/payfor.js
var app = getApp();
var a = app.requirejs("core");
Page({
  data:{
    addressData:{},
    address:false,
    show:false,
    freight:0,
    totalPrice:0
  },
  onLoad: function (options) {
    var self = this;
    wx.showLoading({
        title : "正在加载",
        mask  : true,
    });
    self.setData({
      address: app.goodsInfo.address,
      goodsNum: app.goodsInfo.num,
      goodsInfo: app.goodsInfo,
      goodsPrice: app.goodsInfo.goodsPrice,
      totalPrice: app.goodsInfo.goodsPrice * app.goodsInfo.num,
      show:true
    })
    wx.hideLoading();
       
      
  },
  onShow:function(options){
      wx.showLoading({
        title : "正在加载",
        mask  : true,
      });
    var i = this,
      d = app.getCache("orderAddress");
    if (d) {
      d.userName = d.consigner;
      d.telNumber = d.phone;
      i.setData({
        address: d
      })
    }
      wx.hideLoading();
  },

  showAddressList:function(){
    wx.navigateTo({
      url: '/yb_shop/pages/member/address/select'
    })
  },
  goToPay:function(){
    var self = this;
    if(!this.data.address){
      app.showToast(this,'请选择地址');
      return false;
    }
    var data = {
      pid: self.data.goodsInfo.groupPid,
      isGroup: self.data.goodsInfo.buyType,
      gid: self.data.goodsInfo.id,
      goodsNum: self.data.goodsNum,
      limitTime: self.data.goodsInfo.limitTime,
      address: JSON.stringify(self.data.address),
      totalPrice: (self.data.goodsInfo.goodsPrice * self.data.goodsNum) + parseFloat(self.data.freight),
      uid:app.getCache('userinfo').uid
    }
    if(this.data.oid){
      return;
    }
    wx.showLoading({
      title: '正在提交',
      mask : true,
    })
    a.get('Pintuan/ptCreateOrder',data,function(i){
      wx.hideLoading();
      if(i.code==0){
        self.setData({
          oid:i.info
        })
        a.get('Pintuan/ptPay',{
          oid: i.info,
          openid: getApp().getCache("userinfo").openid
        },function(t){
          if(t.code==0){
            wx.requestPayment({
              'timeStamp': t.info.timeStamp,
              'nonceStr': t.info.nonceStr,
              'package': t.info.package,
              'signType': 'MD5',
              'paySign': t.info.paySign,
              'success': function (res) {
                console.log(res)
                if (data.isGroup == 1) { //拼团
                  // 重定向到团详情页面
                  wx.redirectTo({
                    url: '/yb_shop/pages/pintuan/pages/group/detail?id=' + i.info,
                  })
                  // app.redirect('group/detail','id='+oid)
                } else {
                  // 重定向到订单页面
                  wx.redirectTo({
                    url: '/yb_shop/pages/pintuan/pages/orders/index',
                  })
                  // app.redirect('orders/index','id=3')
                }
              },
              'fail': function (res) {
               a.alert('您已取消了支付',function(){
                 wx.redirectTo({
                   url: '/yb_shop/pages/pintuan/pages/orders/index',
                 })
               })
              }
            })
          }else{
            a.alert(t.msg, function () {
              wx.redirectTo({
                url: '/yb_shop/pages/pintuan/pages/orders/index',
              })
            });
          }
          
        })
      }else{
        a.alert(i.msg)
      }
    })
 
  },

  minus:function(){
    var num = this.data.goodsNum > 1 ? --this.data.goodsNum : 1
    var totalPrice = (this.data.goodsPrice*num);
    this.setData({
      goodsNum : num,
      totalPrice:totalPrice.toFixed(2)
    })
  },
  plus:function(){
    var num = ++this.data.goodsNum
    var totalPrice = (this.data.goodsPrice*num);
    this.setData({
      goodsNum : num,
      totalPrice:totalPrice.toFixed(2)
    })
  }


})